import {observable, action, computed, toJS} from 'mobx';
import {ListAdvanced} from 'weaver-mobile-page';
import { Tools } from 'weaver-mobile';
import objectAssign from "object-assign";

export default class WithDrawStore {

    @observable listAd = new ListAdvanced.Store();
    @observable isinit = false;
    @observable isShowSignInput = false;

    /**
     * 需要提交的参数
     * @type {{doneSk: Array, todoSk: Array, myTodo: string}}
     */
    @observable submitparams = {
        doneSk: [],
        myTodo: "1",
    };

    coreparams={}
    routepath = "/odocComs";
    dataParams = {
        dataUrl: "/api/odoc/withdraw/loadWidthDrawData",
        contionUrl: "/api/odoc/withdraw/loadContionData",
        params:{
            requestid: 10356354,
        },
    };
    workparams={};

    /**
     * 数据改变
     * @param params
     */
    changeSubmitParams = (params={}) =>{
        this.submitparams = {...this.submitparams, ...params}
    }

    /**
     * init
     * @param prepath
     * @param commonParam
     */
    init = (prepath="/odocComs", commonParam={}) => {
        this.workparams  = commonParam;
        let {params} = this.dataParams;
        params = {...params,...commonParam};
        this.dataParams = {...this.dataParams, params};
        this.routepath = prepath;
        this.listAd = new ListAdvanced.Store({route: this.routepath, id: "withdraw"});
        this.isinit = true;
    }

    /**
     * 初始化
     * @param params
     * @returns {Promise<void>}
     */
    @action
    getData = (pparams = {}) => {
        const {dataUrl,params} = this.dataParams;
        pparams.params = {...this.workparams,...pparams.params, ...params };
        this.listAd && this.listAd.getData({
            url: dataUrl,
            method: "POST",
            ...pparams,
        },(res)=>{
            const {showMyTodo,requestid,workflowid} = res;
            this.coreparams = {showMyTodo,requestid, workflowid};
            const myTodo = showMyTodo ? "1" : "0";
            this.changeSubmitParams({myTodo});
        })
    }

    /**
     * 保存数据
     */
    doSave = (selected=[], callback) => {
        this.changeSubmitParams({doneSk:selected});
        let params = {...this.workparams, ...this.submitparams, ...this.coreparams};
        Tools.callApi({
            url:"/api/odoc/withdraw/doWidthDraw",
            method:'POST',
            params
        }).then(res => {
            //保存成功返回上一页
            typeof callback == "function" && callback(res);
        });
    }

    @action
    hiddenSignInput = () => {
        this.isShowSignInput = false;
        // this.sumitParams = {};
    }

    showSignInput = () => {
        this.isShowSignInput = true;
    }

    @action
    changeSubmitParams = (datas) => {
        this.submitparams = objectAssign(
            {}, this.submitparams,
            datas
        );
    }
    appendPickerRemark = (v = "") => {
        const {remark = ""} = this.submitparams;
        let newremark = `${remark}${v}`;
        this.changeSubmitParams({remark: newremark})
    }

    /**
     * 关闭销毁方法
     */
    destory = () => {
        this.submitparams = [];
    }
}